<?php
/**
 * File: ihackyProfileSection.php
 *
 * This file handles the mock ajax calls from the
 * user's profile iHacky section. This files only displays
 * the different classes in the database but each link does
 * not display members, but rather a link to the ihacky homepage.
 */
/**
 *
 */
include 'lib/mockajaxfbconfig.php';
include 'lib/config.php';
include 'iHackyFunctions.php';

//accepts the POST data from the ihacky profile section
if(isset($_REQUEST['userInput'])){

	$expertiseKeyword = $_REQUEST['userInput'];
	$ranktest = $_REQUEST['rank'];
	$conn = getDbConn();
	//mysql built in function to prevent sql injections
	$expertiseKeyword = mysql_real_escape_string($expertiseKeyword);

	$pageNum = 1;
	$rowsPerPage = 25;  //sets the max number of elements per page

	//checks to see if the page value has changed
	if(isset($_REQUEST['page'])){
		$pageNum = $_REQUEST['page'];
	}

	$offset = ($pageNum - 1) * $rowsPerPage;

	$qryGetSearchExpertiseResults = "SELECT class_detail FROM CLASS
	WHERE MATCH (class_name,class_detail) AGAINST ('\"$expertiseKeyword\"' IN BOOLEAN MODE)";

	//query to count the number of rows the search results contain
	$rowCount = mysql_query($qryGetSearchExpertiseResults, $conn) or DIE(mysql_error());
	$numRows = mysql_num_rows($rowCount);
	$maxPage = ceil($numRows/$rowsPerPage);

	//query to display the actual classes on the page
	$qryGetSearchExpertiseResults = "SELECT class_detail FROM CLASS
	WHERE MATCH (class_name,class_detail) AGAINST ('\"$expertiseKeyword\"' IN BOOLEAN MODE)
	LIMIT $offset, $rowsPerPage";

	$result = mysql_query($qryGetSearchExpertiseResults, $conn) or DIE(mysql_error());

	//derive the last number of the page entry
	if($pageNum == $maxPage){
		$lastItem = $offset + ($numRows - (($pageNum - 1) * $rowsPerPage));
	}
	else{
		$lastItem = ($offset + $rowsPerPage);
	}
	$offset++;
	
	
	if(mysql_num_rows($result) > 0){

		$counter = 1;

		echo '
	          <div align="center">
	             <img src="http://www.ikoaguild.com/ihacky/images/ajax-loader.gif" id="loadIcon"
	             style="display: none;"/>
	          </div>';

		echo   "<fb:header icon=\"false\" decoration=\"add_border\">
	            Search returned total of ".$numRows." result(s)...
	          </fb:header><br>";


		echo "<div class=\"header\">";
		echo  "<div align=\"left\">
		<b><font color=\"#3B5998\">
		Displaying results: $offset - $lastItem<br>
		</font></b>
		</div>";
		echo "</div>";
		echo "<div align=\"center\">
		<b><font color=\"#666666\">
		Page $pageNum of $maxPage
		</font></b><br>
		</div>";

		//loop through the search results
		while($resultsArray = mysql_fetch_array($result)){

			$dependencyName = $resultsArray['class_detail'];

			echo "
			<a href=\"http://apps.facebook.com/ihackyapp\">
			$dependencyName
			</a><br>
		      ";   
			$counter++;
		}
		 
		//check to see if user is on first page
		if($pageNum == 1 && ($numRows > $rowsPerPage)){
			$newPageNum = $pageNum + 1;
			echo '<br><br>';
			 
			echo "<div align=\"center\">
			<form id=\"first_page\">
			<input name=\"userInput\" type=\"hidden\" value=\"$expertiseKeyword\">
			<input name=\"page\" type=\"hidden\" value=\"$newPageNum\">
			<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/ihackyProfileSection.php\"
			clickrewriteform=\"first_page\"
			clickrewriteid=\"content\"
			clicktoshow=\"loadIcon\">
			Next
			<img src=\"http://www.ikoaguild.com/ihacky/images/forwardarrow.png\" align=\"top\">
			</a>
			</form>
			</div>";
			 
		}
		//check to see if user is between first or last page
		else if($pageNum < $maxPage){
			echo '<br><br>';
			$previousPageNum = $pageNum - 1;
			echo "<div align=\"center\">
			<table>
			<tr>
			<td>

			<form id=\"pre_page\">
			<input name=\"userInput\" type=\"hidden\" value=\"$expertiseKeyword\">
			<input name=\"page\" type=\"hidden\" value=\"$previousPageNum\">

			<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/ihackyProfileSection.php\"
			clickrewriteform=\"pre_page\"
			clickrewriteid=\"content\"
			clicktoshow=\"loadIcon\">
			<img src=\"http://www.ikoaguild.com/ihacky/images/reversearrow.png\" align=\"top\">
			Previous
			</a>
			</form>

			</td>";
			$nextPageNum = $pageNum + 1;
			echo "<td>
			 
			<form id=\"next_page\">
			<input name=\"userInput\" type=\"hidden\" value=\"$expertiseKeyword\">
			<input name=\"page\" type=\"hidden\" value=\"$nextPageNum\">
			<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/ihackyProfileSection.php\"
			clickrewriteform=\"next_page\"
			clickrewriteid=\"content\"
			clicktoshow=\"loadIcon\">
			Next
			<img src=\"http://www.ikoaguild.com/ihacky/images/forwardarrow.png\" align=\"top\">
			</a>
			</form>
				
			</td>
			</tr>
			</table>
			</div>";
		}
		//check to see if user is on last page
		else if($pageNum == $maxPage && ($numRows > $rowsPerPage)){
			echo '<br><br>';
			$newPageNum = $pageNum - 1;
			echo "<div align=\"center\">
			<form id=\"last_page\">
			<input name=\"userInput\" type=\"hidden\" value=\"$expertiseKeyword\">
			<input name=\"page\" type=\"hidden\" value=\"$newPageNum\">

			<a clickrewriteurl=\"http://www.ikoaguild.com/ihacky/ihackyProfileSection.php\"
			clickrewriteform=\"last_page\"
			clickrewriteid=\"content\"
			clicktoshow=\"loadIcon\">
			<img src=\"http://www.ikoaguild.com/ihacky/images/reversearrow.png\" align=\"top\">
			Previous
			</a>
			</form>
			</div>";
		}

	}
	else{
		echo '
	          <div align="center">
	            <img src="http://www.ikoaguild.com/ihacky/images/ajax-loader.gif" id="loadIcon"
	             style="display: none;"/>
	          </div>';   
		 
		echo "<fb:header icon=\"false\" decoration=\"add_border\">
	       Search returned 0 results...</fb:header><br>";
		 
	}
	closeDb($conn); //close db connection
	exit;
}
?>